package com.huoyun.tag;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

public class DateFormatTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2866174733564730384L;
	
	private Date date;
	
	private String dateFormat;

	@Override
	public int doStartTag() throws JspException 
	{ 
		String writeStr = "";
		
		if(date != null)
		{
			if(StringUtils.isEmpty(dateFormat))
			{
				dateFormat = "yyyy-MM-dd HH:mm:ss";
			}
			
			SimpleDateFormat dateformat =new SimpleDateFormat(dateFormat); 
			
			writeStr = dateformat.format(date);
		}
		
		try 
		{  
		    pageContext.getOut().write(writeStr);  
		} 
		catch (IOException e) 
		{  
			e.printStackTrace();  
		}  
		
		return super.doStartTag();
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getDateFormat() {
		return dateFormat;
	}

	public void setDateFormat(String dateFormat) {
		this.dateFormat = dateFormat;
	}

}
